// Server API makes it possible to hook into various parts of Gridsome
// on server-side and add custom data to the GraphQL data layer.
// Learn more: https://gridsome.org/docs/server-api/

// Changes here require a server restart.
// To restart press CTRL + C in terminal and run `gridsome develop`

const axios = require('axios')

module.exports = function (api) {
  api.loadSource(async ({ addCollection }) => {
    // Use the Data Store API here: https://gridsome.org/docs/data-store-api/
    const collection = addCollection('new')
    let { data } = await axios.get('https://api.github.com/users/GitHub-Laziji/gists?page=1&per_page=1')
    const files = data[0].files
    for(const key in files){
      data[0].url = files[key]['raw_url']
      data[0].title = key
    }

    for (const item of data) {
      collection.addNode({
        id: item.id,
        url: item.url,
        title: item.title,
        created_at: item.created_at,
        updated_at: item.updated_at,
        description: item.description
      })
    }
  })
  api.loadSource(async ({ addCollection }) => {
    // Use the Data Store API here: https://gridsome.org/docs/data-store-api/
    const collection = addCollection('fan')

    let { data } = await axios.get('https://api.github.com/users/GitHub-Laziji/followers')
    for (const item of data) {
      collection.addNode({
        id: item.id,
        followersTotal: data.length,
        name: item.login,
        avatarUrl: item.avatar_url,
        htmlUrl: item.html_url
      })
    }
  })

  api.loadSource(async ({ addCollection }) => {
    // Use the Data Store API here: https://gridsome.org/docs/data-store-api/
    const collection = addCollection('foll')

    let { data } = await axios.get('https://api.github.com/users/GitHub-Laziji/following')
    for (const item of data) {
      collection.addNode({
        followingTotal: data.length,
        name: item.login,
        avatarUrl: item.avatar_url,
        htmlUrl: item.html_url
      })
    }
  })
    
  api.loadSource(async ({ addCollection }) => {
    // Use the Data Store API here: https://gridsome.org/docs/data-store-api/
    const collection = addCollection('blogs')
    let { data } = await axios.get('https://api.github.com/users/GitHub-Laziji/gists')
    for(let i=0;i<data.length;i++) {
      data[i]['title'] = Object.keys(data[i].files)[0]
    }
    for (const item of data) {
      collection.addNode({
        count: data.length,
        url: item.url,
        title: item.title,
        updated_at: item.updated_at,
        description: item.description
      })
    }
  })

  api.loadSource(async ({ addCollection }) => {
    // Use the Data Store API here: https://gridsome.org/docs/data-store-api/
    const collection = addCollection('pro')
    let { data } = await axios.get('https://api.github.com/users/GitHub-Laziji/repos')
    // for(let i=0;i<data.length;i++) {
    //   data[i]['title'] = Object.keys(data[i].files)[0]
    // }
    for (const item of data) {
      collection.addNode({
        count: data.length,
        url: item.url,
        name: item.name,
        updated_at: item.updated_at,
        description: item.description,
        stargazersCount: item.stargazers_count,
        watchersCount: item.watchers_count,
        forksCount: item.forks_count
      })
    }
  })
  
  // api.createPages(({ createPage }) => {
  //   // Use the Pages API here: https://gridsome.org/docs/pages-api/
  // })
}
